package com.user.gamecenter.receiver;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.user.gamecenter.app.MyApp;
import com.user.gamecenter.manager.PlugInManager;
import com.user.gamecenter.manager.UpdateManager;
import com.user.gamecenter.network.help.Error;
import com.user.gamecenter.network.operate.CommonPostOperateRequest;
import com.user.gamecenter.network.original.UpdateAppOriginal;
import com.user.gamecenter.network.request.UpdateAppRequest;
import com.user.gamecenter.network.response.UpdateAppResponse;
import com.user.gamecenter.service.RegularlyDownloadService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cms on 2017/2/8.
 */

public class RegularlyDownloadReceiver extends BroadcastReceiver {

    private static final String TAG = "RegularlyDownloadReceiver";
    private Context context;

    @SuppressLint("LongLogTag")
    @Override
    public void onReceive(Context context, Intent intent) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        this.context = context;
        updateAppReq();
        startRegularlyDownloadService(context);

    }


    private void startRegularlyDownloadService(Context context) {
        Intent intent = new Intent();
        intent.setClass(context.getApplicationContext(), RegularlyDownloadService.class);
        context.getApplicationContext().startService(intent);
    }


    public void updateAppReq() {
        final UpdateAppRequest weChatUpdateAppRequest = new UpdateAppRequest(new UpdateAppResponse(), new UpdateAppOriginal());
        Map<String, Object> weChatUpdateAppMap = new HashMap<>();
        weChatUpdateAppMap.put("version", PlugInManager.getPlugInVersion(context.getApplicationContext(), MyApp.plugInPath));

        weChatUpdateAppRequest.setMap(weChatUpdateAppMap);
        CommonPostOperateRequest.instance().commonPostRequest(weChatUpdateAppRequest);
    }

    @SuppressLint("LongLogTag")
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void updateAppResp(UpdateAppResponse updateAppResponse) {

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        if (!TextUtils.isEmpty(updateAppResponse.getError())) {
            Log.e(TAG, "1------------ =" + updateAppResponse.getError());
            if (updateAppResponse.getError().equals(Error.ERROR_NETWORK_IS_NOT_AVAILABLE)
                    || updateAppResponse.getError().equals(Error.ERROR_NET_TIMEOUT)) {

            }
        } else {
            UpdateAppOriginal updateAppOriginal = updateAppResponse.getData();
            if (updateAppOriginal.getIs_update() == 1) {
                UpdateManager.instance().downloadFile(updateAppOriginal.getPkg().toString());
            }
        }
    }

}
